package babo.cd.physicalexperiment.activity;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;

import java.util.LinkedHashMap;

import babo.cd.physicalexperiment.BaseActivity;
import babo.cd.physicalexperiment.app.MyApplication;
import babo.cd.physicalexperiment.data.UserInfo;
import babo.cd.physicalexperiment.databinding.UpdatepswActivityBinding;
import babo.cd.physicalexperiment.utils.OKCallback;
import babo.cd.physicalexperiment.utils.OkUtils;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;

public class UpdatePasswordActivity extends BaseActivity {
  UpdatepswActivityBinding binding;



    @Override
    protected View bindView() {
        binding=UpdatepswActivityBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {


    }

    @Override
    public void setListener() {
        super.setListener();

        binding.btnSave.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v==binding.btnSave){
            if (TextUtils.isEmpty(binding.edpsw.getText().toString().trim())){
                Out.toast(context,"请输入旧密码！");
                return;
            }
            if (TextUtils.isEmpty(binding.edpswnew.getText().toString().trim())||TextUtils.isEmpty(binding.edpswnew2.getText().toString().trim())){
                Out.toast(context,"请按要求输入新密码！");
                return;
            }
            if (!binding.edpswnew.getText().toString().trim().equals(binding.edpswnew2.getText().toString().trim())){
                Out.toast(context,"两次输入的密码不一致！");
                binding.edpswnew2.setText("");
                return;
            }
            if (binding.edpswnew.getText().toString().trim().length()<6){
                Out.toast(context,"密码长度不低于6位！");

                return;
            }
            String psw=sharedPreferences.getString("psw","");
            if (!psw.equals(binding.edpsw.getText().toString().trim())){
                Out.toast(context,"旧密码错误！");
                return;
            }
            doChange();

        }

    }

    private void doChange() {
        LinkedHashMap hashMap=new LinkedHashMap();
        hashMap.put("id", UserInfo.userId);
        hashMap.put("password",binding.edpswnew.getText().toString().trim());
        OkUtils.performByJsonArray(context, URLS.uploadUserInfo,hashMap,2,new OKCallback(context){
            @Override
            public void Sucess(String str) {
                super.Sucess(str);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("uid","");
                editor.putString("token","");
                editor.putString("psw","");
                editor.commit();
                MyApplication.getInstance().setUserBaseInfo(null);
                UserInfo.JsessionId="";
                UserInfo.userId="";
                Out.toast(context,"修改成功，请重新登录");

                goToNextActivity(LoginActivity.class);
            }
        });
    }


    @Override
    protected void loadData() {

    }

}
